<?php
/**
 * Created by PhpStorm.
 * User: ytl003
 * Date: 2018/9/27
 * Time: 上午12:19
 */

namespace app\api\service;


use app\lib\enum\EnumUser;
use app\lib\exception\BaseException;
use app\lib\exception\PermissionException;
use app\lib\exception\TokenException;
use think\facade\Cache;
use think\facade\Request;

class TokenService
{
    public static function generateToken()
    {
        //32个字符组成一组随机字符串
        $randChars = getRandChar(32);
        //用三组字符串，进行md5加密
        $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
        //salt 盐
        $salt = config('secure.token_salt');

        return md5($randChars . $timestamp . $salt);
    }

    public static function getCurrentTokenVar($key)
    {
        $token = Request::header('token');
        if (!$token)
            $token = Request::cookie('token');
        $vars = Cache::get($token);
        if (!$vars) {
            throw new TokenException();
        } else {
            if (!is_array($vars)) {
                $vars = json_decode($vars, true);
            }
            if ($key == 'all') return $vars;
            if (array_key_exists($key, $vars)) {
                return $vars[$key];
            } else {
                throw new BaseException(['msg' => '尝试获取的Token变量并不存在']);
            }
        }
    }

    public static function verifyToken()
    {
        $token = Request::header('token');
        $exist = Cache::get($token);
        if ($exist) {
            return true;
        } else {
            return false;
        }
    }

    public static function getCurrentUid()
    {
        //token
        $uid = self::getCurrentTokenVar('userId');
        return $uid;
    }

    public static function getCurrentMsg()
    {
        $msg = self::getCurrentTokenVar('all');
        return $msg;
    }

    public static function needAdminPermission(){
        $msg = self::getCurrentTokenVar('all');
        if ($msg['scope'] == EnumUser::ADMIN)
            return true;
        throw new PermissionException();
    }

    public static function needWebPermission(){
        $msg = self::getCurrentTokenVar('all');
        if ($msg['type'] == 'web')
            return true;
        throw new PermissionException();
    }

    public static function needWeiAppPermission(){
        $msg = self::getCurrentTokenVar('all');
        if ($msg['type'] == 'weiApp')
            return true;
        throw new PermissionException();
    }

    public static function needRegisteredPermission(){
        $msg = self::getCurrentTokenVar('all');
        if ($msg['scope'] == EnumUser::REGISTERED)
            return true;
        throw new PermissionException();
    }

    public static function needUserPermission(){
        $msg = self::getCurrentTokenVar('all');
        if ($msg['scope'] == EnumUser::USER)
            return true;
        throw new PermissionException();
    }
    public static function needAllUserPermission(){
        $msg = self::getCurrentTokenVar('all');
        if ($msg['scope'] >= EnumUser::USER)
            return true;
        throw new PermissionException();
    }

}